package org.shiro.service.impl;

import cn.hutool.core.util.StrUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.shiro.basic.entity.Login;
import org.shiro.service.ApproveService;
import org.shiro.utils.ShiroUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 系统认证相关接口实现
 *
 * @author LiKun
 * @date 2021/11/3 10:37
 */
@Service
public class ApproveServiceImpl implements ApproveService, Serializable {

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void login(Login login) {
        Subject subject = SecurityUtils.getSubject();
        subject.login(ShiroUtil.getUserPasswordToken(login));
    }

    @Override
    public void logout(String sessionId) {
        sessionId = StrUtil.isEmpty(sessionId) ? ShiroUtil.getSessionId() : sessionId;
        System.out.println(redisTemplate.delete(ShiroUtil.getSessionKey(sessionId)));
    }

}
